package group2.core.entities;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * This class contains all the data on the comments.
 * 
 * @author Christian
 */
@Entity
public class Comment implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Temporal(TemporalType.DATE)
    @Column(name = "COMMENT_DATE", nullable = false)
    private Date date;
    @ManyToOne
    @JoinColumn(nullable = false, name = "AUTHOR_FK")
    private CamelUser commentAuthor;
    @JoinColumn(nullable = false, name = "POST_FK")
    @ManyToOne
    private Post post;
    @Column(nullable = false, length = 800)
    private String comment;
    private final static int PREVIEW_SIZE = 10;

    public Comment() {
    }

    public Comment(long id, String comment, CamelUser user, Post post) {
        this.id = id;
        this.comment = comment;
        this.commentAuthor = user;
        this.post = post;
        this.date = Calendar.getInstance().getTime();
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getComment() {
        return comment;
    }

    /**
     * Returns a preview of the comment that is PREVIEW_SIZE characters long
     * @return The beginning of the post, for previewing
     */
    public String getCommentPreview() {
        if (this.getComment().length() < PREVIEW_SIZE) {
            return this.getComment();
        } else {
            return this.getComment().substring(0, PREVIEW_SIZE) + "...";
        }


    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Date getDate() {
        return new Date(this.date.getTime());
    }

    public void setDate(Date date) {
        this.date = new Date(date.getTime());
    }

    public CamelUser getCommentAuthor() {
        return commentAuthor;
    }

    public void setCommentAuthor(CamelUser commentAuthor) {
        this.commentAuthor = commentAuthor;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    /**
     * 
     * @param object
     * @return false if objects differ or both are null.
     */
    @Override
    public boolean equals(Object object) {
        //This method will return false is this==null && other==null
        if (object == null || !(object instanceof Comment)) {
            return false;
        }
        Comment other = (Comment) object;

        if (this.id == null && other.id == null) {
            return false;
        }

        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "group2.core.Comment[ id=" + id + " ]";
    }
}